# 抓取网址：http://data.wxb.com/rank
import requests
import json
import urllib.request

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate",
    "Cache-Control": "no-cache",
    "Connection": "close",
    "Cookie": "PHPSESSID=tp0vt9ahpnbku996vvuretjkc0; visit-wxb-id=837b20bccb3f77a1a8e5b6df0c4c4f20; IESESSION=alive; _qddamta_4009981236=3-0; pgv_pvi=2412895232; pgv_si=s7791044608; tencentSig=5037132800; wxb_fp_id=1381177533; Hm_lvt_5859c7e2fd49a1739a0b0f5a28532d91=1504086148; Hm_lpvt_5859c7e2fd49a1739a0b0f5a28532d91=1504087022; _qddaz=QD.sb0qu8.355lwa.j6yu23d8; _qdda=3-1.1; _qddab=3-996f34.j6yu23da",
    "Referer": "http://data.wxb.com/searchResult?kw=%E8%AF%81%E5%88%B8",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.109 Safari/537.36",
    "X-Postman-Interceptor-Id": "6c06f41b-ba61-17a1-9c50-fb545d4753e7",
    "X-Requested-With": "XMLHttpRequest"}

url = 'http://data.wxb.com/search'

# 结果写入文件
flow = open('Wechat public platforms.json', 'w')
mydata = []

for i in range(0, 25):
    myParams = {
        'page': i,
        'page_size': 10,
        'kw': '数据分析',
        'category_id' : '',
        'start_rank': '*',
        'end_rank' : '*',
        'fans_min': '',
        'fans_max': '',
        'sort' : '',
        'is_verify' : 0,
        'is_original' : 0,
        'is_continuous' : 0
        }
    req = requests.get(url,params=myParams, headers=headers)
    print('      这是第'+str(i)+'条数据'+str(req.json().get('data','')),end = ' ')
    # mydata.extend(req.json()['data'])
    mydata.extend(req.json().get('data',''))

json.dump(mydata, flow)
flow.close()
print(len(mydata))